package com.cn.xact.ty_treasured_book.util.xmlparser;

import java.util.Vector;

import com.cn.xact.ty_treasured_book.config.TYConfigBase;

import android.util.Log;

public class ExtXMLItem {

     /**
      * This class is used to store the parsed XML object
      * A standard format for this vector should be :
      * requestName  : name->value->user_id...
      * requestValue : push->20   ->cd100...
      * Thus, method <setRequestName> and <setRequestValue>
      * MUST BE CALLED WITH PAIR wo make sure the value and
      * name match in these 2 vector.
      * Also, if there is index in these 2 methods, THE INDEX
      * MUST BE THE SAME!
      */
     public static final boolean DEBUG = TYConfigBase.XML_DEBUG;

     private int validItemNum = 0;
     private final Vector<String> requestName  = new Vector<String>();
     private final Vector<String> requestValue = new Vector<String>();

     public static final String TAG = "TY_ExtXMLItem";

     public void setItem()
     {
         this.validItemNum++;
     }

     public int getItem()
     {
         return this.validItemNum;
     }
     /**
      * Get the number of the requests items in the vector
      * @param <NONE>
      * @return The number of the requests items.
      */
     public int getRequestLen()
     {
         int reqLen = this.requestName.size();

         if (DEBUG)
             Log.v(TAG, "getRequestLen = " + reqLen);

         return reqLen;
     }

     /**
      * Get the request name by index
      * @param vecIndex  IN  Index of the request name in the vector
      * @return The name of the requests items
      */
     public String getRequestName(int vecIndex) {
         String reqName = this.requestName.get(vecIndex);

         if (DEBUG)
             Log.v(TAG, "getRequestName( " + vecIndex + " ) = " + reqName);

         return reqName;
     }

     /**
      * Set the request name by index
      * @param strReqName  IN  The request name
      * @param iReqIndex   IN  The position the name will be added in
      * @return <NONE>
      */
     public void setRequestName(String strReqName, int iReqIndex) {

         if (DEBUG)
             Log.v(TAG, "setRequestName( " + strReqName + ", " + iReqIndex + " )");

         this.requestName.add(iReqIndex, strReqName);
     }

     /**
      * Set the request name at the tail
      * @param strReqName  IN  The request name
      * @return <NONE>
      */
     public void setRequestName(String strReqName) {
         if (DEBUG)
             Log.v(TAG, "setRequestName( " + strReqName + " )");
         this.requestName.add(strReqName);
     }

     /**
      * Get the request value by index
      * @param vecIndex  IN  Index of the request value in the vector
      * @return The value of the requests items
      */
     public String getRequestValue(int vecIndex) {

         if (DEBUG)
             Log.v(TAG, "getRequestValue( " + vecIndex + " )");

         String reqValue = this.requestValue.get(vecIndex);

         if (DEBUG)
             Log.v(TAG, "getRequestValue( " + vecIndex + " ) = " + reqValue);

         if (null == reqValue)
             return new String();

         return reqValue;
     }

     /**
      * Set the request value by index
      * @param strReqVal  IN  The request value
      * @param iReqIndex   IN  The position the value will be added in
      * @return <NONE>
      */
     public void setRequestValue(String strReqVal, int iReqIndex) {
         if (DEBUG)
             Log.v(TAG, "setRequestValue( " + strReqVal + ", " + iReqIndex + " )");
         this.requestValue.add(iReqIndex, strReqVal);
     }

     /**
      * Set the request value at the tail
      * @param strReqVal  IN  The request value
      * @return <NONE>
      */
     public void setRequestValue(String strReqVal) {
         if (DEBUG)
             Log.v(TAG, "setRequestValue( " + strReqVal + " )");
         this.requestValue.add(strReqVal);
     }

     /**
      * Get the request value by request name
      * @param strName  IN The request name
      * @return The request value match to the request name
      */
     public String getValueByName(String strName)
     {

         if (DEBUG)
             Log.v(TAG, "getValueByName( " + strName + " )" + ifNameExist(strName) + "  " + this.requestName.size());


         int indexInVec = this.requestName.indexOf(strName);

         if (indexInVec < 0) return new String();

         String reqValue = getRequestValue(indexInVec);

         if (DEBUG)
           Log.v(TAG, "getValueByName( " + strName + " ) = " + reqValue);

         return reqValue;
     }

     /**
      * Check whether a string is in a vector
      * @param nameToCheck  IN  String to check
      * @return true is exists, otherwise false
      */
     public boolean ifNameExist(String nameToCheck)
     {
         boolean queryRe = this.requestName.contains(nameToCheck);
         if (DEBUG)
             Log.v(TAG, "ifNameExist( " + nameToCheck + " ) returns " + queryRe);
         return queryRe;
     }

     /**
      * Get number at the tail of the string
      * @param nameWithIndex String with numberic tail
      * @return The index in the tail
      */
     public int getNameIndex(String nameWithIndex)
     {
         int tmpCnt = 1;
         while (true)
         {
             if (nameWithIndex.endsWith(String.valueOf(tmpCnt)))
             {
                 tmpCnt ++;
             }
             else
             {
                 break;
             }
         }
         return (tmpCnt - 1);
     }

     /**
      * Method to print all the items in the vector
      * @param <NONE>
      * @return <NONE>
      */
     @Override
    public String toString(){
        String myRet = null;
        int myRetLen = getRequestLen();
        for (int counter = 0; counter < myRetLen; counter++)
        {
          myRet += "name = "
                 + getRequestName(counter)
                 + "  :  value = "
                 + getRequestValue(counter)
                 + "\n";
        }
          return myRet;
     }
}